package com.controller;

import com.entity.*;
import com.group.ProductionOrderAddGroup;
import com.service.ProducedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/produced")
public class ProducedController {
    @Autowired
    ProducedService producedService;
    //  查询全部生产单
    @PostMapping("/findAll/{pageIndex}/{pageSize}")
    public Result<PageBean<ProductionOrder>> findAll(
            @PathVariable("pageIndex") int pageIndex,
            @PathVariable("pageSize") int pageSize,
            @RequestBody ProductionOrder productionOrder){
        PageBean<ProductionOrder> productionOrderPageBean = producedService.findAll(pageIndex, pageSize, productionOrder);
        return Result.ok(productionOrderPageBean);
    }
    //  查询全部生产单位
    @RequestMapping ("/findAllDept")
    public Result<List<Department>> findAll(){
        return Result.ok(producedService.findAllDept());
    }
    //  创建生产单
    @PostMapping("/add")
    public Result<Void> add(@Validated(ProductionOrderAddGroup.class) @RequestBody ProductionOrder productionOrder) {
        producedService.add(productionOrder);
        return Result.ok();
    }
    //  修改生产单
    @PostMapping("/modify")
    public Result<Void> modify(@RequestBody ProductionOrder productionOrder) {
        producedService.modify(productionOrder);
        return Result.ok();
    }
    //  删除生产单
    @PostMapping("/delete")
    public Result<Void> delete(@RequestBody ProductionOrder productionOrder) {
        producedService.delete(productionOrder);
        return Result.ok();
    }
    //  批量删除
    @PostMapping("/deletebatchget")
    public Result<Void> delete(@RequestBody DeleteBatch deleteBatch) {
        producedService.deleteBatch(deleteBatch.getIds());
        return Result.ok();
    }


    //  查询全部生产领料
    @PostMapping("/findAllReceive/{pageIndex}/{pageSize}")
    public Result<PageBean<ProductionReceive>> findAllReceive(
            @PathVariable("pageIndex") int pageIndex,
            @PathVariable("pageSize") int pageSize,
            @RequestBody ProductionReceive productionReceive){
        PageBean<ProductionReceive> productionReceivePageBean = producedService.findAllReceive(pageIndex, pageSize, productionReceive);
        return Result.ok(productionReceivePageBean);
    }
    //  创建生产领料
    @PostMapping("/addReceive")
    public Result<Void> addReceive(@RequestBody ProductionReceive productionReceive) {
        producedService.addReceive(productionReceive);
        return Result.ok();
    }
    //  修改生产领料
    @PostMapping("/modifyReceive")
    public Result<Void> modifyReceive(@RequestBody ProductionReceive productionReceive) {
        producedService.modifyReceive(productionReceive);
        return Result.ok();
    }
    //  删除生产领料
    @PostMapping("/deleteM")
    public Result<Void> deleteM(@RequestBody ProductionReceive productionReceive) {
        producedService.deleteM(productionReceive);
        return Result.ok();
    }
    //  get批量删除生产领料
    @PostMapping("/deletebatchgetM")
    public Result<Void> deleteBM(@RequestBody DeleteBatch deleteBatch) {
        producedService.deleteBatchM(deleteBatch.getIds());
        return Result.ok();
    }


    //  查询全部生产情况
    @PostMapping("/findAllProduced/{pageIndex}/{pageSize}")
    public Result<PageBean<ProductionProduced>> findAllProduced(
            @PathVariable("pageIndex") int pageIndex,
            @PathVariable("pageSize") int pageSize,
            @RequestBody ProductionProduced productionProduced){
        PageBean<ProductionProduced> productionProducedPageBean = producedService.findAllProduced(pageIndex, pageSize,productionProduced);
        return Result.ok(productionProducedPageBean);
    }
    //  查询全部产品
    @RequestMapping ("/findAllGoods")
    public Result<List<ProductionGoods>> findAllGoods(){
        return Result.ok(producedService.findAllGoods());
    }
    //  查询全部原料
    @RequestMapping ("/findAllRaws")
    public Result<List<ProductionRaws>> findAllRaws(){
        return Result.ok(producedService.findAllRaws());
    }
    //  根据生产单查询产品、原料及适宜生产员工
    @PostMapping("/findOrderByPoid/{id}")
    public Result<Map<String, List<String>>> getGoodsRawsByOrder(@PathVariable("id") int id){
        return Result.ok(producedService.findGoodsRawsByOrder(id));
    }
    //  根据产品id查询所需原料
    @PostMapping("/findrawByGoods/{id}")
    public Result<List<String>> getByEid(@PathVariable("id") int id){
        return Result.ok(producedService.findRawsByGoods(id));
    }
    //  查询全部未生产单
    @RequestMapping ("/findAllPoids")
    public Result<List<ProductionOrder>> findAllPoids(){
        return Result.ok(producedService.findAllPoids());
    }
    //  查询全部生产员工
    @RequestMapping ("/findAllEmployee")
    public Result<List<ProductionEmployee>> findAllEmployees(){
        return Result.ok(producedService.findAllEmployees());
    }
    //  开始生产
    @PostMapping("/addProduced")
    public Result<Void> add(@RequestBody ProductionProduced productionProduced) {
        //  获取生产单id
        int id = productionProduced.getOrder().getId();
        //  根据id获取产品及生产原料
        Map<String, List<String>> order = producedService.findGoodsRawsByOrder(id);
        //  根据生产员工名字获取id
        List<ProductionEmployee> employees = producedService.findEmployeeByName(productionProduced.getEmployee().getEname());
        for (ProductionEmployee employee:employees) {
            if (employee.getDid() == producedService.findOrderById(id).getDepartment().getDid()){
                productionProduced.setEmployee(employee);
            }
        }
        //  得到产品名字
        List<String> strings = order.get(productionProduced.getOrder().getPname());
        //  得到生产原料
        for (String s:strings) {
            if (!s.equals(strings.get(0)) && !s.equals(strings.get(1))){
                //  获取原料名字
                String sname = s.split("：")[0];
                //  获取原料数量及单位
                String s1 = s.split("：")[1];
                System.out.println("原料名字："+sname);
                System.out.println("原料数量："+s1);
                int i=0;
                if (s1.contains("吨")){
                    i = s1.indexOf("吨");
                }else {
                    i = s1.indexOf("千克");
                }
                //  获取所需原料数量
                String s2 = s1.substring(0, i);
                //  获取所需原料单位
                String s3 = s1.substring(i);
                //  修改原料库存
                ProductionStore productionStore = new ProductionStore(sname,s2,s3,null,"原料",null,null,null);
                producedService.modifyStore(productionStore);
            }
        }
        producedService.addProduced(productionProduced);
        producedService.modifyPstate(productionProduced);
        return Result.ok();
    }
    //  删除生产情况
    @PostMapping("/deleteP")
    public Result<Void> deleteP(@RequestBody ProductionProduced productionProduced) {
        System.out.println("删除："+productionProduced);
        producedService.deleteProduced(productionProduced);
        return Result.ok();
    }
    //  批量删除生产情况
    @PostMapping("/deletebatchgetP")
    public Result<Void> deleteBP(@RequestBody DeleteBatch deleteBatch) {
        producedService.deleteBatchProduced(deleteBatch.getIds());
        return Result.ok();
    }
    //  查询生产情况
    @PostMapping("/findProduced/{id}")
    public Result<ProductionProduced> findProducedById(@PathVariable("id") int id){
        return Result.ok(producedService.findProducedById(id).get(0));
    }
    //  修改生产情况
    @PostMapping("/modifyProduced")
    public Result<Void> modifyProduced(@RequestBody ProductionProduced productionProduced) {
        producedService.modifyProduced(productionProduced);
        return Result.ok();
    }
    //  结束生产
    @PostMapping("/overProduced")
    public Result<Void> overProduced(@RequestBody ProductionProduced productionProduced) {
        //  成品存储
        String sname = productionProduced.getOrder().getPname();
        String snum = productionProduced.getOrder().getPrnum();
        String sunit = productionProduced.getOrder().getPrunit();
        //  修改原料库存
        ProductionStore productionStore = new ProductionStore(sname,snum,sunit,null,"成品",null,null,null);
        producedService.modifyStore(productionStore);
        producedService.overProduced(productionProduced);
        return Result.ok();
    }


    //  查询全部完成生产的情况
    @PostMapping("/findAllProducedOver/{pageIndex}/{pageSize}")
    public Result<PageBean<ProductionProduced>> findAllProducedOver(
            @PathVariable("pageIndex") int pageIndex,
            @PathVariable("pageSize") int pageSize,
            @RequestBody ProductionProduced productionProduced){
        PageBean<ProductionProduced> productionProducedPageBean = producedService.findAllProducedOver(pageIndex, pageSize,productionProduced);
        System.out.println(productionProducedPageBean);
        return Result.ok(productionProducedPageBean);
    }
    //  查询生产仓储
    @PostMapping("/findProducedStore")
    public Result<List<ProductionStore>> findProducedStore(){
        return Result.ok(producedService.findAllProducedStore());
    }

    //  仓储入库
    @PostMapping("/addStore")
    public Result addStore(@RequestBody ProductionStore productionStore){
        producedService.addStore(productionStore);
        return Result.ok();
    }
}
